package com.bitcoin.scheduler.task;

import com.bitcoin.scheduler.domain.blockcc.KLineBlockCC;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.assertj.core.util.Lists;
import org.jsoup.Jsoup;
import org.junit.Test;

import java.io.IOException;
import java.math.BigDecimal;
import java.net.URISyntaxException;
import java.util.Date;
import java.util.List;

@Slf4j
public class Time {
    @Test
    public void kline() throws IOException, URISyntaxException {
        String[] markets = {"gate-io", "binance"};
        String[] symbols = {
            "QTUM",
            "BTC",
            "ETH",
            "XRP",
            "EOS",
            "ADA",
            "LTC",
            "XLM",
            "TRX",
            "NEO",
            "ETC",
            "BNB",
            "ONT"
        };
        for (String symbol : symbols) {
            for (String market : markets) {
                try {
                    log.info("开始同步{} -----{}", market, symbol);
                    String response = Jsoup.connect("https://data.block.cc/api/v1/kline?symbol_pair=" + symbol + "_USDT&market=" + market + "&limit=50000" +
                        "" + market).ignoreContentType(true)
                        .proxy("localhost", 1080)
                        .timeout(5 * 60 * 1000)
                        .execute().body();
                    if (!StringUtils.endsWith(response, "}")) {
                        response = response.substring(0, StringUtils.lastIndexOf(response, "],") + 1) + "]}";
                    }
                    log.info("数据读取完毕 {} -----{}", market, symbol);
                    ObjectMapper mapper = new ObjectMapper();
                    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
                    mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true);
                    mapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
                    mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
                    mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
                    KlineResponse klineResponse = mapper.readValue(response, KlineResponse.class);
                    List dataList = klineResponse.getData();
                    List<KLineBlockCC> lineList = Lists.newArrayList();
                    for (Object data : dataList) {
                        List info = (List) data;
                        KLineBlockCC line = new KLineBlockCC();
                        line.setCurrency("USDT");
                        line.setPriceTime(new Date((long) info.get(0)));
                        line.setSymbol(symbol);
                        line.setMarket(market);
                        line.setOpenPrice(BigDecimal.valueOf(NumberUtils.toDouble(info.get(1).toString())));
                        line.setClosePrice(BigDecimal.valueOf(NumberUtils.toDouble(info.get(2).toString())));
                        line.setLowPrice(BigDecimal.valueOf((NumberUtils.toDouble(info.get(3).toString()))));
                        line.setHighPrice(BigDecimal.valueOf((NumberUtils.toDouble(info.get(4).toString()))));
                        line.setVolume(BigDecimal.valueOf(NumberUtils.toDouble(info.get(5).toString())));
                        lineList.add(line);
                    }
                    log.info("{} -----{}", market, symbol);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }
            break;
        }
    }

    @Test
    public void time() {
       String s =  DateFormatUtils.format(DateUtils.addMinutes(new Date(), -33000 * 5),"yyyyMMdd hh:mm:ss");
       System.out.print(s);
    }
}
